package com.shaoshaohuo.app.view;

import java.util.ArrayList;
import java.util.List;
import com.shaoshaohuo.app.R;
import com.shaoshaohuo.app.entity.EcCategoryThird;
import com.shaoshaohuo.app.adapter.VarietyGridviewAdapter;
import com.shaoshaohuo.app.utils.ToastUtil;

import android.content.Context;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.GridView;
import android.widget.PopupWindow;
import android.widget.PopupWindow.OnDismissListener;
import android.widget.AdapterView.OnItemClickListener;

/**
 * Description:
 */
public class SelectProductTypePop implements OnClickListener {

    private Context context;
    private PopupWindow hintPopWinwow;

    private OnSelectItemListener onSelectItemListener;

    private GridView mDataGv;
    private Button mNextButton;
    private List<EcCategoryThird> actions;
    protected int selectPosition = -1;
    
    private ArrayList<EcCategoryThird> mSelectVarietyList = new ArrayList<>();
	private VarietyGridviewAdapter<Object> mAdapter;
    
    public SelectProductTypePop(Context context) {
        this.context = context;
        initView();
    }

    private void initView() {
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        final View contentView = inflater.inflate(R.layout.select_product_type_dialog, null, false);
        hintPopWinwow = new PopupWindow(contentView, WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT,
                        true);
        hintPopWinwow.setOutsideTouchable(true);
        hintPopWinwow.setBackgroundDrawable(new BitmapDrawable());
        hintPopWinwow.setAnimationStyle(R.style.Anim_Dialog_Bottom);

        mDataGv = (GridView) contentView.findViewById(R.id.gv_list);
        mNextButton = (Button) contentView.findViewById(R.id.button_next);
        
        
        mNextButton.setOnClickListener(this);
        
        mDataGv.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            	EcCategoryThird variety = (EcCategoryThird) parent.getItemAtPosition(position);
                if(variety.isSelect()){
                    mSelectVarietyList.remove(variety);
                    variety.setSelect(false);
                }else{
                    mSelectVarietyList.add(variety);
                    variety.setSelect(true);
                }
                mAdapter.notifyDataSetChanged();
            }
        });
    }
    
    public void setData(List<EcCategoryThird> list) {
    	this.actions = list;
    	if(mAdapter == null){
    		mAdapter = new VarietyGridviewAdapter<>(context, actions, false);
    		mAdapter.setSingleSeletion(false);
    		mDataGv.setAdapter(mAdapter);
    	}else{
    		mAdapter.setData(list);
    	}
	}

    public void showLocation(View parent) {
         hintPopWinwow.showAtLocation(parent, Gravity.BOTTOM, 0, 0);
    }

    public boolean isShowing() {
        return hintPopWinwow.isShowing();
    }

    public void setOnDismissListener(OnDismissListener onDismissListener){
        hintPopWinwow.setOnDismissListener(onDismissListener);
    }
    
    public void setOnSelectItemListener(OnSelectItemListener onSelectDateListener) {
        this.onSelectItemListener = onSelectDateListener;
    }

    public interface OnSelectItemListener {

        void onSelect(List<EcCategoryThird> list);
    }
    
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.button_next:
            if(mSelectVarietyList.isEmpty()){
                ToastUtil.toast("请选择品种");
            }else{
                hintPopWinwow.dismiss();
                if(onSelectItemListener != null){
                    onSelectItemListener.onSelect(mSelectVarietyList);
                }
            }
            break;
        }
    }

}
